package com.common.app.helper

import com.common.app.data.bean.KeyEvents
import com.common.app.data.bean.user.UserBean
import com.common.app.utls.Constants
import com.common.app.data.bean.KeyPre
import com.common.app.data.bean.user.LoginBean
import com.common.base.app.extras.no
import com.common.base.app.extras.yes
import com.common.base.data.CommonBean
import com.common.base.utils.SpUtilsManagement
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/25
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
object UserHelper {

    //用户是否已经登录
    fun isLogin(): Boolean {
        return Constants.userBean != null
    }

    //检查是否已经登录
    fun checkLogin(): Boolean {
        val isLogin = isLogin()
        isLogin.no { LaunchHelper.launchLogin() }
        return isLogin
    }

    fun getToken(): String {
        Constants.curToken.isEmpty().yes {
            Constants.curToken = SpUtilsManagement.getString(KeyPre.KEY_USER_TOKEN, "")
        }

        return Constants.curToken
    }

    fun getUser(): UserBean? {
        if (Constants.userBean == null) {
            Constants.userBean = SpUtilsManagement.getParcelable(KeyPre.KEY_USER_BEAN, UserBean::class.java)
        }
        return Constants.userBean
    }

    fun getUserPhone(): String {

        Constants.curPhone.isNullOrEmpty().yes {
            Constants.curPhone = SpUtilsManagement.getString(KeyPre.KEY_USER_PHONE, "")
        }
        return Constants.curPhone
    }

    fun getUserDbName(): String {
        return getUser()?.user_id.toString()
    }

    fun getUid(): String {
        return getUser()?.getId() ?: "-101"
    }

    fun init() {
        //解析保存的用户token
        Constants.curTkToken = SpUtilsManagement.getString(KeyPre.KEY_USER_TOKEN, "")

        //解析保存的用户数据对象
        Constants.userBean = SpUtilsManagement.getParcelable(KeyPre.KEY_USER_BEAN, UserBean::class.java)

        getUserPhone()
    }

    //登录成功
    fun loginSuccess(loginBean: CommonBean<LoginBean>) {
        loginBean.Data?.let {
            Constants.curToken = it.Token
            Constants.curTkToken = it.Token
            SpUtilsManagement.putString(KeyPre.KEY_USER_TOKEN, it.Token)
        }
        loginBean.Data?.Profile?.let {
            Constants.userBean = it
            it.phone?.let { phoneIt ->
                Constants.curPhone = phoneIt
            }
            SpUtilsManagement.putParcelable(KeyPre.KEY_USER_BEAN, it)
            UmengAgentHelper.onProfileSignIn(it.mobile?:"")
        }
    }

    //退出登录
    fun logout() {
        clearUsrInfo()
        Constants.userBean = null
        Constants.curToken = ""
        Constants.curTkToken = ""
//        LiveEventBus.get(KeyEvents.KEY_USER_LOGIN_SUCCESS, Boolean::class.java).post(false)
    }

    /**
     * 是否是自己
     */
    fun isSelf(uid: String): Boolean {
        return uid == getUid()
    }

    /**
     * 更新用户对象
     */
    fun updateUser(user: UserBean?, newUser: UserBean?) {
        newUser?.apply {
            user?.let { user ->
                user.avatar_url = avatar_url
                user.nickname = nickname
                user.avatar = avatar
                user.description = description
                user.phone = phone
                user.birthday = birthday
                user.height = height
                user.weight = weight
                user.sex = sex
                updateUser(user)
                LiveEventBus.get(KeyEvents.KEY_REFRESH_USER_INFO_DATA).post(this)
            }
        }
    }

    fun updateUser(user: UserBean? = Constants.userBean) {
        user?.let {
            SpUtilsManagement.putParcelable(KeyPre.KEY_USER_BEAN, it)
        }
    }


    /**
     * 清空缓存个人信息
     */
    private fun clearUsrInfo() {
        // clear token
        SpUtilsManagement.removeFromKey(KeyPre.KEY_USER_TOKEN)
        SpUtilsManagement.removeFromKey(KeyPre.KEY_USER_BEAN)
        SpUtilsManagement.removeFromKey(KeyPre.KEY_DOWN_CLOUD_DATA_PROGRESS)
    }
}